package com.rural.bus.driver;

import java.util.Date;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.rural.bus.driver.config.BusConstant;
import com.rural.bus.driver.config.MyPreferenceManager;
import com.rural.bus.driver.config.OrderType;
import com.rural.bus.driver.config.PreferenceKey;
import com.rural.bus.driver.fragment.MyOrderFragment;
import com.rural.bus.driver.model.Order;
import com.rural.bus.driver.net.MyHttpUtil;
import com.rural.bus.driver.net.OnNetCallback;
import com.rural.bus.driver.net.Url;
import com.rural.bus.driver.utils.DateUtil;
import com.rural.bus.driver.utils.ProgressUtil;
import com.rural.bus.driver.view.CommonAlertDialog;
import com.rural.bus.driver.view.ModifyDistanceDialog;

public class OrderConfirmActivity extends BaseActivity implements OnClickListener {

	private Order order;
	private double navigateDistance;
	private String userId;

	private TextView tv_fee_value, tv_time, tv_distance;
	private ImageView iv_modify_distance;
	private Button btn_confirm_fee;
	private LinearLayout ll_distance;

	private ModifyDistanceDialog dialog = null;
	private float distance;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_order_confirm);

	}

	@Override
	protected void initView() {

		mBtMiddle.setVisibility(View.VISIBLE);
		mBtMiddle.setText("订单确认");
		mBtMiddle.setTextColor(getResources().getColor(R.color.white));

		mImgLeft.setVisibility(View.VISIBLE);
		mImgLeft.setBackgroundResource(R.drawable.back_bg_selector);

		tv_fee_value = (TextView) findViewById(R.id.tv_fee_value);
		btn_confirm_fee = (Button) findViewById(R.id.btn_confirm_fee);
		iv_modify_distance = (ImageView) findViewById(R.id.iv_modify_distance);
		tv_time = (TextView) findViewById(R.id.tv_time);
		tv_distance = (TextView) findViewById(R.id.tv_distance);
		ll_distance = (LinearLayout) findViewById(R.id.ll_distance);

		btn_confirm_fee.setOnClickListener(this);
		iv_modify_distance.setOnClickListener(this);
	}

	@Override
	protected void initData() {
		userId = MyApplication.driverInfo.drivId;
		order = (Order) getIntent().getSerializableExtra("order");
		distance = MyPreferenceManager.instance().getSharedInfoByKey(PreferenceKey.GO_DISTANCE, 0.0f);

		tv_distance.setText(distance + " 公里");
		
//		long time = (new Date().getTime() - order.takeTime);
//		toast(""+time);
//		tv_time.setText(DateUtil.getTimeDistance(time / 1000));
	}
	
	protected void setListeners() {

		mImgLeft.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				finish();
			}
		});
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_confirm_fee:
			arriveEndLocation();
			break;
		case R.id.iv_modify_distance:
			if (dialog == null) {
				dialog = new ModifyDistanceDialog(this, "确定", "取消", "", new ModifyDistanceDialog.OnInputDialogDismissListener() {
					@Override
					public void onConfirmClicked(String input) {
						distance = Long.parseLong(input);
						tv_distance.setText(distance + " 公里");
						dialog.dismiss();
					}

					@Override
					public void onCancelClicked() {
						dialog.dismiss();
					}
				});
				dialog.setOwnerActivity(this);
			}
			dialog.show();
			break;
		default:
			break;
		}

	}

	private void arriveEndLocation() {

		MyHttpUtil http = new MyHttpUtil();
		http.addBodyParm("orderId", "" + order.orderId);
		http.addBodyParm("groupId", "" + order.groupId);
		http.addBodyParm("driverId", "" + order.driverId);

		if (distance < 0.1) {
			toast("请检查行驶公里数");
			return;
		}

		http.addBodyParm("distance", "" + distance);
		ProgressUtil.showProgress(this, "");
		http.send(Url.confirm_url, new OnNetCallback() {

			@Override
			public void success(JSONObject object) {
				// {"message":null,"order":{"createTime":1449630353000,"endLatitude":40.047862,"remarks":null,"finalPrice":null,"carType":0,"endLongtitude":116.363094,"distance":10000,"endLocation":"北领地c区","userId":4,"driverId":1,"userName":"马杰","userPhone":"13112345678","startLongtitude":116.324178,"driverName":"陈馨(宝马湘A**897)","orderId":130,"startLatitude":40.089919,"orderType":1,"number":1,"driverPhone":"13333333331","perDistance":1.5,"takeTime":11100000,"startLocation":"专家国际公馆","orderStatus":2,"payId":null,"groupId":8881611,"payStatus":null,"estimatePrice":15000},"dirverSize":null,"callStatus":0}
				ProgressUtil.closeProgress();
				try {
					if (object.get("status").equals("0")) {
						object = object.getJSONObject("result");
						if (object != null) {
							order = new Gson().fromJson(object.getString(("order")), new TypeToken<Order>() {
							}.getType());
							MyOrderFragment.isFresh = true;
							MyPreferenceManager.instance().putSharedInfoOfFloat(PreferenceKey.GO_DISTANCE, 0.0f);
							showFeeDialog();
						}
					} else {
						toast(R.string.data_error);
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}

			@Override
			public void fail(String error) {
				toast("" + error);
				ProgressUtil.closeProgress();
			}
		});
	}
	
	
	public void showFeeDialog(){
		CommonAlertDialog dialog1 = new CommonAlertDialog(this, "确定", null, "用车费用：" + order.estimatePrice + "元，已发送乘客，请耐心等待乘客支付！" , new CommonAlertDialog.OnDialogDismissListener() {
			@Override
			public void onConfirmClicked() {
				Intent intent = new Intent(OrderConfirmActivity.this, MainActivity.class);
				startActivity(intent);
				OrderConfirmActivity.this.finish();
			}

			@Override
			public void onCancelClicked() {

			}
		});
		dialog1.setOwnerActivity(this);
		dialog1.show();
	}
}
